package com.fanyue.modules.wzb.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.fanyue.core.base.BaseServiceImpl;
import com.fanyue.core.cache.CoolCache;
import com.fanyue.core.exception.CoolPreconditions;
import com.fanyue.core.request.R;
import com.fanyue.core.util.CoolSecurityUtil;
import com.fanyue.core.util.DatabaseDialectUtils;
import com.fanyue.modules.base.entity.sys.BaseSysParamEntity;
import com.fanyue.modules.base.entity.sys.BaseSysUserEntity;
import com.fanyue.modules.base.service.sys.BaseSysParamService;
import com.fanyue.modules.base.service.sys.BaseSysUserService;
import com.fanyue.modules.wzb.entity.*;
import com.fanyue.modules.wzb.mapper.CargoMapper;
import com.fanyue.modules.wzb.mapper.ClothingBatchMapper;
import com.fanyue.modules.wzb.mapper.ClothingInfoMapper;
import com.fanyue.modules.wzb.service.*;
import com.fanyue.modules.wzb.utils.CodeUtils;
import com.fanyue.modules.wzb.vo.ClothingBatchVO;
import com.fanyue.modules.wzb.vo.CountBatchVO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.tangzc.autotable.core.constants.DatabaseDialect;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.fanyue.modules.base.entity.sys.table.BaseSysDepartmentEntityTableDef.BASE_SYS_DEPARTMENT_ENTITY;
import static com.fanyue.modules.base.entity.sys.table.BaseSysUserEntityTableDef.BASE_SYS_USER_ENTITY;
import static com.fanyue.modules.wzb.entity.table.AppUserEntityTableDef.APP_USER_ENTITY;
import static com.fanyue.modules.wzb.entity.table.CargoEntityTableDef.CARGO_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingBatchEntityTableDef.CLOTHING_BATCH_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingRecordEntityTableDef.CLOTHING_RECORD_ENTITY;


/**
 * 系统用户
 */
@Service
@RequiredArgsConstructor
public class ClothingBatchServiceImpl extends BaseServiceImpl<ClothingBatchMapper, ClothingBatchEntity> implements ClothingBatchService {

    final private CoolCache coolCache;
    final private BaseSysParamService sysParamService;
    final private ClothingBatchItemService clothingBatchItemService;

    @Override
    public Object page(JSONObject requestParams, Page<ClothingBatchEntity> page, QueryWrapper qw) {
        String keyWord = requestParams.getStr("keyWord");
        Integer status = requestParams.getInt("status");
        String dateStartTime = requestParams.getStr("dateStartTime");
        String dateEndTime = requestParams.getStr("dateEndTime");

        String databaseDialect = DatabaseDialectUtils.getDatabaseDialect();
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
        } else {
            qw.select(CLOTHING_BATCH_ENTITY.ALL_COLUMNS,
                    BASE_SYS_USER_ENTITY.NAME.as("sysUserName"));
        }
        qw.from(CLOTHING_BATCH_ENTITY)
                .leftJoin(BASE_SYS_USER_ENTITY)
                .on(CLOTHING_BATCH_ENTITY.SYS_USER_ID.eq(BASE_SYS_USER_ENTITY.ID));

        if (StrUtil.isNotEmpty(keyWord)) {
            qw.and(CLOTHING_BATCH_ENTITY.CODE.like(keyWord)
                .or(CLOTHING_BATCH_ENTITY.PEOPLE.like(keyWord))
                .or(CLOTHING_BATCH_ENTITY.MOBILE.like(keyWord))
                .or(CLOTHING_BATCH_ENTITY.OPERATOR.like(keyWord))
                .or(CLOTHING_BATCH_ENTITY.APPROVER.like(keyWord))
                .or(CLOTHING_BATCH_ENTITY.ORG.like(keyWord)));
        }

        qw.and(CLOTHING_BATCH_ENTITY.STATUS.eq(status, status != null));
        if (StrUtil.isNotEmpty(dateStartTime)) {
            qw.and(CLOTHING_BATCH_ENTITY.CREATE_TIME.ge(dateStartTime));
        }
        if (StrUtil.isNotEmpty(dateEndTime)) {
            qw.and(CLOTHING_BATCH_ENTITY.CREATE_TIME.le(dateEndTime));
        }

        qw.groupBy(CLOTHING_BATCH_ENTITY.ID);
        return mapper.paginate(page, qw);
    }

    @Override
    public void replaceIsBatch(Integer isBatch) {
        BaseSysParamEntity baseSysParamEntity = sysParamService.getByKey("clothingIsBatch");
        if (baseSysParamEntity == null) {
            //创建
            baseSysParamEntity = new BaseSysParamEntity();
            baseSysParamEntity.setKeyName("clothingIsBatch");
            baseSysParamEntity.setName("被装批量操作开关");
            baseSysParamEntity.setData(isBatch.toString());
            baseSysParamEntity.setDataType(0);
            baseSysParamEntity.setRemark("后台是否在批量操作被装");
            sysParamService.add(baseSysParamEntity);
        } else {
            //修改
            baseSysParamEntity.setData(isBatch.toString());
            sysParamService.updateById(baseSysParamEntity);
        }
    }

    @Override
    public void updateStatusByItem(List<ClothingBatchItemEntity> itemList) {
        //
        List<Long> ids = new ArrayList<>();
        for (ClothingBatchItemEntity item: itemList) {
            boolean isAdd = true;
            for (Long id : ids) {
                if (id.equals(item.getBatchId())) {
                    isAdd = false;
                    break;
                }
            }
            if (isAdd) {
                ids.add(item.getBatchId());
            }
        }
        for (Long id: ids) {
            ClothingBatchEntity batchEntity = getById(id);

            ClothingBatchItemEntity selectItem = new ClothingBatchItemEntity();
            selectItem.setBatchId(id);
            selectItem.setStatus(0);
            List<ClothingBatchItemEntity> item0List = clothingBatchItemService.listByInfo(selectItem);
            if (item0List.size() == 0) {
                //说明已经还完了
                batchEntity.setStatus(1);
            } else {
                selectItem.setStatus(1);
                List<ClothingBatchItemEntity> item1List = clothingBatchItemService.listByInfo(selectItem);
                if (item1List.size() > 0) {
                    //还了部分
                    batchEntity.setStatus(2);
                }
            }
            updateById(batchEntity);
        }
    }

    @Override
    public R countByBatch(JSONObject requestParams) {
        Long batchId = requestParams.getLong("batchId", 0L);
        List<CountBatchVO> list = mapper.countByBatch(batchId);
        return R.ok(list);
    }

}